package org.misalen.web.service.push;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.misalen.web.service.file.impl.OssFileImpl;
import org.springframework.stereotype.Service;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

@Service
public class JPushImpl {
	private JPushClient client;

	static Properties p = new Properties();
	static {
		InputStream in = OssFileImpl.class.getClassLoader().getResourceAsStream("pro.properties");
		try {
			p.load(in);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public JPushImpl() {
		String masterSecret = p.getProperty("push.jiguang.masterSecret");
		String appKey = p.getProperty("push.jiguang.appKey");
		client = new JPushClient(masterSecret, appKey);

	}

	// new Thread(new Runnable() {

	//

	// @Override

	// public void run() {

	// PushParameter parameter = new PushParameter();

	// parameter.setNotification(true);

	// parameter.setType("1234121111");

	// parameter.setToken("160a3797c805f384a94");

	// parameter.setTitle("tongzhi");

	// new JPushImpl().sendPush(parameter);

	// }

	// }).start();

	//

	// }

	public Map<String, Object> sendPush(PushParameter parameter) {
		try {
			if (parameter.isNotification()) {
				if (parameter.getToken() == null) {
					client.sendPush(buildPushAllAlert(parameter.getTitle(), parameter.getType()));
				} else {
					client.sendPush(buildPushAAlert(parameter.getTitle(), parameter.getType(), parameter.getToken()));
				}
			} else {
				if (parameter.getToken() == null) {
					client.sendPush(buildPushAllMsg(parameter.getType()));
				} else {
					PushResult pushResult = client.sendPush(buildPushAMsg(parameter.getType(),parameter.getExtra(), parameter.getToken()));
					System.err.println(pushResult.isResultOK());
				}
			}
		} catch (APIConnectionException e) {
			System.err.println(e.toString());
		} catch (APIRequestException e) {
			System.err.println(e.toString());
		}
		return null;
	}

	private static PushPayload buildPushAllMsg(String type) {
		return PushPayload.newBuilder()//
				.setPlatform(Platform.all())//
				.setAudience(Audience.all())//
				.setMessage(//
						Message.newBuilder().setMsgContent("sxwh")//
								.addExtra("type", type).build())//
				.build();

	}

	private static PushPayload buildPushAllAlert(String title, String type) {
		return PushPayload.newBuilder()//
				.setPlatform(Platform.all())//
				.setAudience(Audience.all())//
				.setNotification(Notification.alert(title))//
				.setMessage(//
						Message.newBuilder().setMsgContent("sxwh")//
								.addExtra("type", type).build())//
				.build();
	}

	private static PushPayload buildPushAMsg(String type, Map<String, String> map, List<String> list) {
		return PushPayload.newBuilder()//
				.setPlatform(Platform.all())//
				.setAudience(Audience.alias(list))//
				.setMessage(//
						Message.newBuilder().setMsgContent("sxwh")//
								.addExtra("type", type).addExtras(map).build())//

				.build();

	}

	private static PushPayload buildPushAAlert(String title, String type, List<String> list) {
		return PushPayload.newBuilder()//
				.setPlatform(Platform.all())//
				.setAudience(Audience.alias(list))//
				.setNotification(Notification.alert(title))
				.setMessage(//
						Message.newBuilder().setMsgContent("sxwh")//
								.addExtra("type", type).build())//

				.build();
	}

}
